#include "include/TaskQueue.hpp"
#include "include/ThreadPool.hpp"
shared_ptr<task> TaskQueue::pop(){
    _mutex.lock();
    while(empty()&&!ThreadPool::isExit){
        _notempty.wait(_mutex);
    } 
    shared_ptr<task> ptask;
    if(!empty()){
    ptask = task_queue.front();
    task_queue.pop();

    }
    _notfull.notify_one();
    _mutex.unlock();
    return ptask;

}

void TaskQueue::push(task * Task){
    _mutex.lock();
    while(full()){
        _notfull.wait(_mutex);
    }
    task_queue.push(shared_ptr<task>(Task));
    _notempty.notify_one();
    _mutex.unlock();
}

bool TaskQueue::full(){
    return size()>=_capacity;
}

bool TaskQueue::empty(){
    return size()<=0;
}

int TaskQueue::size(){
    return task_queue.size();
}

void TaskQueue::wake(){
     _notempty.notify_all();
}
